/**
 * 
 */
package com.ffcs.crmd.tsp.api.dto.task;

import java.io.Serializable;

import com.thoughtworks.xstream.annotations.XStreamAliasType;

/**
 * 功能说明:优先级权重
 *
 * @author ZHONGFUHUA
 * 
 * @since 2.0.0-SNAPSHOT
 *
 */
@XStreamAliasType("PriorityWeight")
public class PriorityWeight implements Serializable {
    
    /**
     * 
     */
    private static final long serialVersionUID    = -6596714400449403766L;
    
    public final static int   PRIORITY_FIVE       = 5;
    public final static int   PRIORITY_FOUR       = 4;
    public final static int   PRIORITY_THREE      = 3;
    public final static int   PRIORITY_TWO        = 2;
    public final static int   PRIORITY_ONE        = 1;
    
    /**
     * 5级权重
     */
    private volatile int      priorityWeightFive  = 0;
    
    /**
     * 4级权重
     */
    private volatile int      priorityWeightFour  = 0;
    
    /**
     * 3级权重
     */
    private volatile int      priorityWeightThree = 0;
    
    /**
     * 2级权重
     */
    private volatile int      priorityWeightTwo   = 0;
    
    /**
     * 1级权重
     */
    private volatile int      priorityWeightOne   = 100;
    
    /**
     * 
     * 功能说明:获取权重
     * 
     * @param priority 优先级
     * @return 权重
     */
    public float getPriorityWeight(int priority) {
        switch (priority) {
            case PRIORITY_FIVE:
                return Float.valueOf(priorityWeightFive) / getTotalWeight();
            case PRIORITY_FOUR:
                return Float.valueOf(priorityWeightFour) / getTotalWeight();
            case PRIORITY_THREE:
                return Float.valueOf(priorityWeightThree) / getTotalWeight();
            case PRIORITY_TWO:
                return Float.valueOf(priorityWeightTwo) / getTotalWeight();
            case PRIORITY_ONE:
                return Float.valueOf(priorityWeightOne) / getTotalWeight();
            default:
                return -1;
        }
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @return
     */
    private float getTotalWeight() {
        int total = priorityWeightFive + priorityWeightFour + priorityWeightThree + priorityWeightTwo + priorityWeightOne;
        if (total > 0) {
            return Float.valueOf(total);
        } else {
            this.priorityWeightOne = 100;
            return 100;
        }
    }
    
    /**
     * 
     * 功能说明:设置权重
     * 
     * @param priority 优先级
     * @param weight 权重
     */
    public void setPriorityWeight(int priority, int weight) {
        switch (priority) {
            case PRIORITY_FIVE:
                this.priorityWeightFive = weight;
                break;
            case PRIORITY_FOUR:
                this.priorityWeightFour = weight;
                break;
            case PRIORITY_THREE:
                this.priorityWeightThree = weight;
                break;
            case PRIORITY_TWO:
                this.priorityWeightTwo = weight;
                break;
            case PRIORITY_ONE:
                this.priorityWeightOne = weight;
                break;
        }
    }
    
}
